def add(x, y):
    return x + y


def subtract(x, y):
    return x - y


def multiply(x, y):
    return x * y


def divide(x, y):
    if y == 0:
        return "错误：除数不能为零"
    return x / y


def calculator():
    print("选择运算：")
    print("1. 加法")
    print("2. 减法")
    print("3. 乘法")
    print("4. 除法")

    while True:
        try:
            choice = input("请输入运算符 (1/2/3/4)，或输入 'exit' 退出：")

            if choice == 'exit':
                print("退出计算器。")
                break

            if choice not in ('1', '2', '3', '4'):
                print("无效输入，请选择 1、2、3 或 4。")
                continue

            num1 = float(input("请输入第一个数字："))
            num2 = float(input("请输入第二个数字："))

            if choice == '1':
                print(f"{num1} + {num2} = {add(num1, num2)}")
            elif choice == '2':
                print(f"{num1} - {num2} = {subtract(num1, num2)}")
            elif choice == '3':
                print(f"{num1} * {num2} = {multiply(num1, num2)}")
            elif choice == '4':
                print(f"{num1} / {num2} = {divide(num1, num2)}")

        except ValueError:
            print("输入无效，请输入数字。")


if __name__ == "__main__":
    calculator()
